package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanHealthInformationEntity;
import city.spring.modules.oldman.service.OldmanHealthInformationService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人健康信息
 *
 * @author 侯坤林
 * @date 2020-05-20 22:51:29
 */
@RestController
@RequestMapping("/oldman_health_information")
public class OldmanHealthInformationController extends ApiController {
    private final OldmanHealthInformationService oldmanHealthInformationService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanHealthInformationEntity, ?>> entityOrderFields;

    public OldmanHealthInformationController(OldmanHealthInformationService oldmanHealthInformationService) {
        this.oldmanHealthInformationService = oldmanHealthInformationService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanHealthInformationEntity::getId);
        entityOrderFields.add(OldmanHealthInformationEntity::getOldmanId);
        entityOrderFields.add(OldmanHealthInformationEntity::getSelfCare);
        entityOrderFields.add(OldmanHealthInformationEntity::getVision);
        entityOrderFields.add(OldmanHealthInformationEntity::getHearing);
        entityOrderFields.add(OldmanHealthInformationEntity::getAttendingHospital);
        entityOrderFields.add(OldmanHealthInformationEntity::getAttendingPhysician);
        entityOrderFields.add(OldmanHealthInformationEntity::getTelephone);
        entityOrderFields.add(OldmanHealthInformationEntity::getAllergicDrugs);
        entityOrderFields.add(OldmanHealthInformationEntity::getMedicalHistory);
        entityOrderFields.add(OldmanHealthInformationEntity::getMajorDiseases);
    }

    /**
     * 获取全部的 <strong>老人健康信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, OldmanHealthInformationEntity entity) {
        LambdaQueryChainWrapper<OldmanHealthInformationEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人健康信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, OldmanHealthInformationEntity entity) {
        LambdaQueryChainWrapper<OldmanHealthInformationEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanHealthInformationEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>老人健康信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanHealthInformationService.getById(id));
    }

    /**
     * 添加一个 <strong>老人健康信息</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanHealthInformationEntity entity) {
        oldmanHealthInformationService.saveOldmanHealthInformation(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人健康信息</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanHealthInformationEntity entity) {
        entity.setId(id);
        oldmanHealthInformationService.updateOldmanHealthInformation(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人健康信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanHealthInformationService.deleteOldmanHealthInformation(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人健康信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanHealthInformationService.deleteOldmanHealthInformation(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanHealthInformationEntity> buildLambdaQuery(OldmanHealthInformationEntity entity) {
        LambdaQueryChainWrapper<OldmanHealthInformationEntity> lambdaQuery = oldmanHealthInformationService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanHealthInformationEntity::getId, entity.getId());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getOldmanId()), OldmanHealthInformationEntity::getOldmanId, entity
                .getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSelfCare()), OldmanHealthInformationEntity::getSelfCare, entity
                .getSelfCare());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getVision()), OldmanHealthInformationEntity::getVision, entity.getVision());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getHearing()), OldmanHealthInformationEntity::getHearing, entity.getHearing());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAttendingHospital()), OldmanHealthInformationEntity::getAttendingHospital, entity
                .getAttendingHospital());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAttendingPhysician()), OldmanHealthInformationEntity::getAttendingPhysician, entity
                .getAttendingPhysician());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTelephone()), OldmanHealthInformationEntity::getTelephone, entity
                .getTelephone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAllergicDrugs()), OldmanHealthInformationEntity::getAllergicDrugs, entity
                .getAllergicDrugs());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMedicalHistory()), OldmanHealthInformationEntity::getMedicalHistory, entity
                .getMedicalHistory());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMajorDiseases()), OldmanHealthInformationEntity::getMajorDiseases, entity
                .getMajorDiseases());
        return lambdaQuery;
    }

}
